package cc.eddic.examinationsystem.web;

import cc.eddic.examinationsystem.core.BaseController;
import cc.eddic.examinationsystem.domain.Exam;
import cc.eddic.examinationsystem.domain.Student;
import cc.eddic.examinationsystem.dto.ExamModel;
import cc.eddic.examinationsystem.dto.ExamScoreModel;
import cc.eddic.examinationsystem.repository.ExamRepository;
import cc.eddic.examinationsystem.repository.ExamScoreRepository;
import cc.eddic.examinationsystem.repository.StudentExamRepository;
import cc.eddic.examinationsystem.repository.StudentRepository;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping("/studentExam")

public class StudentExamController extends BaseController {
    public StudentExamController(StudentExamRepository studentExamRepository, ExamScoreRepository examScoreRepository, ExamRepository examRepository) {
        this.studentExamRepository = studentExamRepository;
        this.examScoreRepository = examScoreRepository;
        this.examRepository = examRepository;
    }

    @GetMapping("/select_score")
    public String selectScore(Model model) {
        model.addAttribute("tip", "null");
        return "score-select";
    }

    @GetMapping("/score")
    public String score(@RequestParam String text, Model model) {
        if (text.length() == 0) {
            model.addAttribute("tip", "您还未输入员工号！");
            return "score-select";
        }
        long employeeId = Long.parseLong(text);
        if (examScoreRepository.findAll().stream().filter(examScore -> examScore.getStudent().getEmployeeNo().equals(employeeId)).collect(Collectors.toList()).size() == 0) {
            if (log.isDebugEnabled()) log.debug("员工号不存在！");
            model.addAttribute("tip", "您查找的数据不存在！");
            return "score-select";
        }
        List<ExamScoreModel> examScoreModels = examScoreRepository.findAll().stream().filter(examScore -> examScore.getStudent().getEmployeeNo().equals(employeeId)).map(ExamScoreModel::from).collect(Collectors.toList());
        String name = studentRepository.findAll().stream().filter(student -> student.getEmployeeNo().equals(employeeId)).findFirst().map(Student::getName).orElseThrow(NoSuchFieldError::new);
        System.out.printf("***************" + examScoreModels.size());
        model.addAttribute("examScoreModels", examScoreModels);
        model.addAttribute("name", name);
        model.addAttribute("tip", "null");
        return "score";
    }

    @GetMapping("/waitingRoom")
    public String waitingRoom(Model model, @RequestParam("examId") Long examId) {
        val optionalExam = examRepository.findById(examId);
        model.addAttribute("startTime", optionalExam.get().getExamAt());
        model.addAttribute("examId", examId);
        model.addAttribute("studentId", super.currentUser().getId());
        return "exam-waitingRoom";
    }


    @GetMapping("/list")
    public String listForm(Model model) {
        model.addAttribute("examList1", examScoreRepository.findAll().stream().map(ExamScoreModel::from).collect(Collectors.toList()));
        model.addAttribute("examList2", studentExamRepository.findAll().stream().map(ExamModel::from).collect(Collectors.toList()));
        return "student-exam";
    }


    private final StudentExamRepository studentExamRepository;
    private final ExamScoreRepository examScoreRepository;

    @Autowired
    private StudentRepository studentRepository;
    private final ExamRepository examRepository;


}
